home *** CD-ROM | disk | FTP | other *** search
/ Macworld Expo - Develope…Central & Net Innovations / Developer Central and Net Innovators (MacWorld Expo) (January 1999).iso / Developer Central / Bowers Development / Demo AppMaker / Examples / plain C OS8 / Gadgets / MainMenu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-29  |  5.1 KB  |  257 lines  |  [TEXT/CWIE]

  1. /* MainMenu.c */
  2.  
  3. #include <Types.h>
  4. #include <Quickdraw.h>
  5. #include <Controls.h>
  6. #include <Dialogs.h>
  7. #include <Events.h>
  8. #include <Lists.h>
  9. #include <LowMem.h>
  10. #include <Menus.h>
  11. #include <TextEdit.h>
  12. #include <ToolUtils.h>
  13.  
  14. #include "Globals.h"
  15. #include "ResourceDefs.h"
  16. #include "Miscellany.h"
  17. #include "AMApp.h"
  18. #include "AMEngine.h"
  19. #include "AMWindow.h"
  20. #include "Dispatcher.h"
  21. #include "GadgetsDoc.h"
  22. #include "MainMenu.h"
  23.  
  24.  
  25. /*----------*/
  26. static long        GetCommandFromMenu (long        menuChoice);
  27. static void        DoApple            (short            itemNr);
  28. static void        Enable            (short            itemNr,
  29.                                  Boolean        enabled);
  30. static void        EnableTitle        (MenuHandle        menu,
  31.                                  Boolean        enabled);
  32.  
  33. /*----------*/
  34. void    InitTitles (void)
  35. {
  36. } /*InitTitles*/
  37.  
  38. /*----------*/
  39. void    LoadMenus (void)
  40. {
  41.     AppleMenu    = GetMenu (MENU_Apple);
  42.     FailNilResource ((Handle)AppleMenu);
  43.     AppendResMenu (AppleMenu, 'DRVR');
  44.     FileMenu    = GetMenu (MENU_File);
  45.     EditMenu    = GetMenu (MENU_Edit);
  46.     PopupMenu    = GetMenu (MENU_Popup);
  47.     BevelMenu    = GetMenu (MENU_Bevel);
  48.  
  49.     InsertMenu (AppleMenu, 0);
  50.     InsertMenu (FileMenu, 0);
  51.     InsertMenu (EditMenu, 0);
  52.  
  53.     DrawMenuBar ();
  54. } /*LoadMenus*/
  55.  
  56. //----------
  57. long    GetCommandFromMenu (
  58.     long        menuChoice)
  59. {
  60.     long        commandID = 0;
  61.  
  62.     switch (menuChoice) {
  63.         case cAppleAbout:
  64.                 commandID = cmdInvokeAbout;
  65.             break;
  66.         case cFileNew:
  67.                 commandID = cmdNew;
  68.             break;
  69.         case cFileOpen:
  70.                 commandID = cmdOpen;
  71.             break;
  72.         case cFileClose:
  73.                 commandID = cmdClose;
  74.             break;
  75.         case cFileSave:
  76.                 commandID = cmdSave;
  77.             break;
  78.         case cFileSaveAs:
  79.                 commandID = cmdSaveAs;
  80.             break;
  81.         case cFileRevert:
  82.                 commandID = cmdRevert;
  83.             break;
  84.         case cFilePageSetup:
  85.                 commandID = cmdPageSetup;
  86.             break;
  87.         case cFilePrint:
  88.                 commandID = cmdPrint;
  89.             break;
  90.         case cFileQuit:
  91.                 commandID = cmdQuit;
  92.             break;
  93.         case cEditUndo:
  94.                 commandID = cmdUndo;
  95.             break;
  96.         case cEditCut:
  97.                 commandID = cmdCut;
  98.             break;
  99.         case cEditCopy:
  100.                 commandID = cmdCopy;
  101.             break;
  102.         case cEditPaste:
  103.                 commandID = cmdPaste;
  104.             break;
  105.         case cEditClear:
  106.                 commandID = cmdClear;
  107.             break;
  108.         case cEditSelectAll:
  109.                 commandID = cmdSelectAll;
  110.             break;
  111.         case cEditShowClipboard:
  112.                 commandID = cmdShowClipboard;
  113.             break;
  114.  
  115.         default:
  116.                 commandID = -menuChoice;
  117.     }
  118.  
  119.     return commandID;
  120. }
  121.  
  122. //----------
  123. void    DoApple (
  124.     short        itemNr)
  125. {
  126.     Str255            name;
  127.     short            refNum;
  128.  
  129.     GetMenuItemText (AppleMenu, itemNr, name);
  130.     refNum = OpenDeskAcc (name);
  131. }
  132.  
  133. /*----------*/
  134. void    DoMenu (
  135.     long        menuChoice)
  136. {
  137.     long            commandID;
  138.     AMDoc*            curDoc;
  139.     short            menuID;
  140.     short            itemNr;
  141.  
  142.     commandID = GetCommandFromMenu (menuChoice);
  143.     curDoc = cur->mDoc;
  144.     if (DoCommand (cur, commandID)) {    // Dispatch
  145.         // cur window handled it
  146.     } else if ((curDoc != nil)
  147.     && (DoDocCommand (curDoc, commandID))) {
  148.         // document handled it
  149.     } else if (DoAppCommand (gApplication, commandID)) {
  150.         // application handled it
  151.     } else {
  152.         menuID = HiWord (menuChoice);
  153.         itemNr = LoWord (menuChoice);
  154.         if (menuID == MENU_Apple) {
  155.             DoApple (itemNr);
  156.         }
  157.     }
  158.  
  159.     HiliteMenu (0);
  160. } /*DoMenu*/
  161.  
  162. /*----------*/
  163. MenuHandle        menu;
  164. Boolean            menuBarChanged;
  165.  
  166. /*----------*/
  167. static void Enable    (short        itemNr,
  168.                      Boolean    enabled)
  169. {
  170.     if (enabled) {
  171.         EnableItem  (menu, itemNr);
  172.     } else {
  173.         DisableItem (menu, itemNr);
  174.     }
  175. } /*Enable*/
  176.  
  177. /*----------*/
  178. static void EnableTitle    (MenuHandle        menu,
  179.                          Boolean        enabled)
  180. {
  181.     if (enabled != ((**menu).enableFlags & 1)) {
  182.         menuBarChanged = true;
  183.     }
  184.     if (enabled) {
  185.         EnableItem  (menu, 0);
  186.     } else {
  187.         DisableItem (menu, 0);
  188.     }
  189. } /*EnableTitle*/
  190.  
  191. /*----------*/
  192. void    UpdateMenus (void)
  193. {
  194.     WindowPeek        frontPeek;
  195.     Boolean            isFront;        /*is there a front window?*/
  196.     Boolean            isCur;            /*is there a current window?*/
  197.     Boolean            isCurDoc;        /*is there a current document?*/
  198.     Boolean            isDirty;        /*is it dirty?*/
  199.     Boolean            hasFile;        /*does it have a file?*/
  200.     Boolean            isSelected;        /*is anything selected?*/
  201.     Boolean            isDesk;            /*is the front window a desk acc?*/
  202.     Boolean            isText;            /*is there a current text field?*/
  203.     Boolean            isScrap;        /*is there any scrap?*/
  204.     TEHandle        curTE;
  205.  
  206.     menuBarChanged = false;
  207.  
  208.     isFront        = (FrontWindow () != nil);
  209.     isCur        = (curWindow != nil);
  210.     isDirty        = false;
  211.     hasFile        = false;
  212.     isSelected    = false;
  213.     isCurDoc    = (cur->mDoc != nil);
  214.     if (isCurDoc) {
  215.         isDirty        = AMEngine_IsDirty (cur->mDoc->mEngine);
  216.         hasFile        = AMEngine_HasFile (cur->mDoc->mEngine);
  217.     }
  218.  
  219.     isDesk = false;
  220.     if (isFront) {
  221.         frontPeek    = (WindowPeek) FrontWindow ();
  222.         isDesk        = (frontPeek->windowKind < 0);
  223.     }
  224.  
  225.     curTE = AMWindow_GetCurTE (cur);
  226.  
  227.     isText        = (curTE != nil);
  228.     isScrap        = false;
  229.     if (isText) {
  230.         isSelected    = ((**curTE).selStart != (**curTE).selEnd);
  231.         isScrap        = (TEGetScrapLength () > 0);
  232.     }
  233.  
  234.     menu = FileMenu;
  235.     Enable (cFileClose,        isFront);
  236.     Enable (cFileSave,        isDirty);
  237.     Enable (cFileSaveAs,    isCur);
  238.     Enable (cFileRevert,    isDirty);
  239.  
  240.     menu = EditMenu;
  241.     if (isFront) {
  242.         Enable (cEditUndo,        isDesk);
  243.         Enable (cEditCut,        isDesk || isSelected);
  244.         Enable (cEditCopy,        isDesk || isSelected);
  245.         Enable (cEditPaste,        isDesk || isScrap);
  246.         Enable (cEditClear,        isDesk || isSelected);
  247.         Enable (cEditSelectAll,    isText);
  248.  
  249.     }
  250.     EnableTitle (EditMenu,     isFront);
  251.  
  252.  
  253.     if (menuBarChanged) {
  254.         DrawMenuBar ();
  255.     }
  256. } /*UpdateMenus*/
  257.